package cn.edu.xmu.oomall.share.dao;

import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.share.mapper.SharePoMapper;
import cn.edu.xmu.oomall.share.mapper.SuccessfulSharePoMapper;
import cn.edu.xmu.oomall.share.model.bo.Share;
import cn.edu.xmu.oomall.share.model.bo.SuccessfulShare;
import cn.edu.xmu.oomall.share.model.po.SharePo;
import cn.edu.xmu.oomall.share.model.po.SharePoExample;
import cn.edu.xmu.oomall.share.model.po.SuccessfulSharePo;
import cn.edu.xmu.oomall.share.model.po.SuccessfulSharePoExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

import static cn.edu.xmu.privilegegateway.annotation.util.Common.cloneVo;

/**
 * @author Jianxiang Xie
 * @date 2021/12/5
 */

@Repository
public class ShareDao {

    private static final Logger logger = LoggerFactory.getLogger(ShareDao.class);
    @Autowired
    SharePoMapper sharePoMapper;
    @Autowired
    SuccessfulSharePoMapper successfulSharePoMapper;

    /**
     * 利用传入的Bo插入插入数据库
     *
     * @param share ShareBo
     * @return
     */
    public ReturnObject insertShare(Share share) {
        try {
            SharePo sharePo = cloneVo(share, SharePo.class);
            sharePoMapper.insertSelective(sharePo);
            return new ReturnObject(cloneVo(sharePo, Share.class));
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }

    /**
     * 根据传入的条件返回用ReturnObject包装的PageInfo<SharePo>
     *
     * @param example  条件example
     * @param page     页
     * @param pageSize 页大小
     * @return ReturnObject包装的PageInfo<SharePo>
     */
    public ReturnObject getSharePosByExampleWithPage(SharePoExample example, Integer page, Integer pageSize) {
        try {
            PageHelper.startPage(page, pageSize);
            List<SharePo> sharePos = sharePoMapper.selectByExample(example);
            List<Share> shares = new ArrayList<>();

            for (SharePo sharePo : sharePos) {
                shares.add(cloneVo(sharePo, Share.class));
            }
            PageInfo pageInfo = PageInfo.of(sharePos);
            pageInfo.setList(shares);
            return new ReturnObject(pageInfo);
        } catch (Exception e) {
            logger.error("getSharePosByExampleWithPage:error in getSharePosByExampleWithPage");
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }

    /**
     * 不分页查找SharePo
     *
     * @param example 条件example
     * @return ReturnObject包装的List<SharePo>
     */
    public ReturnObject getSharePosByExample(SharePoExample example) {
        try {
            List<SharePo> sharePos = sharePoMapper.selectByExample(example);
            List<Share> shares = new ArrayList<>();
            for (SharePo sharePo : sharePos) {
                shares.add(cloneVo(sharePo, Share.class));
            }
            return new ReturnObject(shares);
        } catch (Exception e) {
            logger.error("getSharePosByExample:error in getSharePosByExample");
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }

    /**
     * 根据传入的条件返回用ReturnObject包装的PageInfo<SuccessfulSharePo>
     *
     * @param example  条件example
     * @param page     页
     * @param pageSize 页大小
     * @return ReturnObject包装的PageInfo<SuccessfulSharePo>
     */
    public ReturnObject getSuccessfulSharePosByExampleWithPage(SuccessfulSharePoExample example, Integer page, Integer pageSize) {
        try {
            PageHelper.startPage(page, pageSize);
            List<SuccessfulSharePo> sharePos = successfulSharePoMapper.selectByExample(example);
            List<SuccessfulShare> successfulShares = new ArrayList<>();
            for (SuccessfulSharePo successfulSharePo : sharePos) {
                successfulShares.add(cloneVo(successfulSharePo, SuccessfulShare.class));
            }
            PageInfo pageInfo = PageInfo.of(sharePos);
            pageInfo.setList(successfulShares);
            return new ReturnObject(pageInfo);
        } catch (Exception e) {
            logger.error("getSuccessfulSharePosByExampleWithPage:error in getSuccessfulSharePosByExampleWithPage");
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }

    /**
     * 不分页查找SuccessfulShare
     *
     * @param example
     * @return
     */
    public ReturnObject getSuccessfulSharePosByExample(SuccessfulSharePoExample example) {
        try {
            List<SuccessfulSharePo> sharePos = successfulSharePoMapper.selectByExample(example);
            List<SuccessfulShare> successfulShares = new ArrayList<>();
            for (SuccessfulSharePo successfulSharePo : sharePos) {
                successfulShares.add(cloneVo(successfulSharePo, SuccessfulShare.class));
            }
            return new ReturnObject(successfulShares);
        } catch (Exception e) {
            logger.error("getSuccessfulSharePosByExample:error in getSuccessfulSharePosByExample");
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }

    /**
     * 利用Po更新分享成功数据库
     *
     * @param successfulSharePo Po
     * @return
     */
    public ReturnObject updateSuccessfulShareByPo(SuccessfulSharePo successfulSharePo) {
        try {
            int ret = successfulSharePoMapper.updateByPrimaryKeySelective(successfulSharePo);
            if (ret != 1) {
                return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR);
            }
            return new ReturnObject();
        } catch (Exception e) {
            logger.error("updateSuccessfulShareByExample:error in updateSuccessfulShareByExample");
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }

    /**
     * 利用Po更新Share表
     *
     * @param sharePo Po
     * @return
     */

    public ReturnObject updateShareByPo(SharePo sharePo) {
        try {
            int ret = sharePoMapper.updateByPrimaryKeySelective(sharePo);
            if (ret != 1) {
                return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR);
            }
            return new ReturnObject();
        } catch (Exception e) {
            logger.error("updateShareByPo:error in updateShareByPo");
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }
}
